<?php
/**
 * Created by PhpStorm.
 * User: HP_USER
 * Date: 2018/6/5
 * Time: 16:24
 */
namespace app\admin\controller;

use app\common\Common;
use think\Db;

class Member extends Base
{
    //统计报表
    public function statistics_member($data){
        if(empty(cache($data))){
            return json(['code'=>$this->f_code,'msg'=>"无效"]);
        }
        $array =  unserialize(cache("statement"));
        if($array == ""){
        $parameter = unserialize(Common::db_count_id("merchant_and_commission","para","drh_parameter"));
        $jiaozi = unserialize(Common::db_count_id("copulation","jiaozi","drh_parameter"));
        //会员人数统计
        $array['member']['membership'] = Common::db_count_id('COUNT(id)',"`count`","drh_user_info");
        //创始会员数量
        $array['member']['initiate'] = Common::db_count_id("COUNT(id)","`count`","drh_user_info","user_lv=1");
        //商户数量总计
        $array['commercial']['number'] = Common::db_count_id("COUNT(id)","`count`","drh_shop");
        //线上商户
        $array['commercial']['on_line'] = Common::db_count_id("COUNT(id)","`count`","drh_shop","id_card=''");
        //线下商户
        $array['commercial']['offline'] = Common::db_count_id("COUNT(id)","`count`","drh_shop","id_card !=''");
        //今年商品交易总金额
        $add_time = Common::getTime("Y");//2019
        $end_time = Common::getTime('Y',true);//2020
        $array['commodity_transaction']['amount_of_trade'] = round(Common::summation(Common::db_count("price","","drh_goods_order","pay_status != 0 AND ".$add_time."<order_start_time"),"price"));
        //今年交易总单量
        $array['commodity_transaction']['total'] = Common::db_count_id("COUNT(id)","`count`","drh_goods_order",$add_time."<order_start_time");
        //当月交易总量
        $month = Common::getTime("m");
        $array['commodity_transaction']['trading'] = Common::summation(Common::db_count("price","","drh_goods_order","pay_status != 0 AND ".$month." <= order_start_time"),"price");
        //当月交易总单数
        $array['commodity_transaction']['the_singular'] = Common::db_count_id("COUNT(id)","`count`","drh_goods_order",$month."<order_start_time");
        //交易手续费营收
        $array['commodity_transaction']['service'] = round(Common::summation(Common::db_count("fees","","drh_user_flow","type_status=1"),"fees"));
        //腾讯扣除
        $array['commodity_transaction']['tencent']  =   round($array['commodity_transaction']['service'] *0.006);
        //商户推荐人抽佣
        $drh_shop = implode(",",Common::er_array(Common::db_count("user_id","","drh_shop")));
        $referr = Common::db_count("*","","drh_user_friend","friend_id IN(".$drh_shop.")");
        $frequency = count($referr);
        $para =$parameter['brokerage']??0;
        $array['commodity_transaction']['commission'] = bcmul($frequency,$para);
        //平台毛利
        $array['commodity_transaction']['margin'] = round($array['commodity_transaction']['service'] - $array['commodity_transaction']['tencent']-$array['commodity_transaction']['commission']);
        //交易产生交子数量
        $jiao = Common::er_array(Common::db_count("return_integral","","drh_user_flow"));
        foreach ($jiao as $k=>$v){
            if($v>0){
             $da[]= $v;
            }
        }
        $array['jiaozi']['production'] = array_sum($da);
        //已质押交子数量
        $array['jiaozi']['pledge'] = array_sum(Common::er_array(Common::db_count("integral","","drh_integral_withdraw","end_time>".time())));
        //平台回收交子数量
        $array['jiaozi']['recycle'] =array_sum(Common::er_array(Common::db_count("integral","","drh_integral_withdraw_flow","type_status=1")));   ;
        //平台流通交子数量
        $array['jiaozi']['circulate'] = array_sum(Common::er_array(Common::db_count("user_integral","user_integral","drh_user_info")));
        //质押交子收益
       $a =  Common::db_count("*","","drh_integral_withdraw");
       foreach ($a as $k=>$v){
           $b[] = date("n",$a[$k]['end_time']-$a[$k]['create_time']);
       }
       $interest = [12=>$jiaozi['interest_12'],9=>$jiaozi['interest_9'],6=>$jiaozi['interest_6'],3=>$jiaozi['interest_3']];
       foreach ($b as $key=>$val){
           foreach ($interest as $k=>$v){
               if($val >= $k){
                   $accrual[] =  bcmul($a[$key]['integral'],$v/100,2);
               }
           }
       }
        $array['jiaozi']['earnings'] = round(array_sum($accrual));
        //回收交子收益
        $array['jiaozi']['recycling_benefits'] = bcmul($jiaozi['sum'], $array['jiaozi']['pledge']);
        //商户收取服务费费总额
        $order =array_count_values(Common::er_array(Common::db_count("shop_id","","drh_goods_order")));
        foreach ($order as $k=>$v){
            if($v>$parameter['amount']){
                $single[] = $k;
            }
        }
        $single = implode(',',$single);
        $payment = Db::table("drh_shop_payment_rule")->where('shop_id',"in",$single)->where('status',1)->field('id')->count('id');
        $array['merchant_handling_fee']['charge'] = round($payment*$parameter['service_charge']);
        //推荐人商户提成
        $percentage = bcmul(bcmul(array_sum(Common::er_array(Common::db_count("price","","drh_goods_order","shop_id IN(".$drh_shop.")"))),0.01),0.1);
        $array["merchant_handling_fee"]['referrals'] =$percentage;
        //渠道提成总额
        $array['merchant_handling_fee']['channel'] = 0;
        //平台运营成本
        $array['merchant_handling_fee']['platform_cost'] = bcmul(Common::db_count_id("COUNT(id)","`count`","drh_shop"),config("cost"));
        //平台手续费毛利
        $array['merchant_handling_fee']['platform_margin'] =  $array['merchant_handling_fee']['charge']-$percentage-$array['merchant_handling_fee']['channel']-$array['merchant_handling_fee']['platform_cost'];
        //供货商总数
        $array['supplier']['number'] = Common::db_count_id("COUNT(id)","`count`","drh_proxy_shop_account");
        //供货商出货量
        $ship = implode(",",Common::er_array(Common::db_count("goods_id","","drh_goods_proxy")));
        $shop_goods = implode(",",Common::er_array(Common::db_count("id","","drh_goods","is_ware=1 AND id IN(".$ship.")")));//
        $shop_order =Common::db_count("price,goods_num","","drh_goods_order","goods_id IN(".$shop_goods.")");//Common::er_array();
            $price = array();
            $goods_num = array();
            foreach ($shop_order as $key =>$val){
            $price[] = $val['price'];
            $goods_num[] = $val['goods_num'];
        }
        $array['supplier']['shipment'] = array_sum($goods_num);
        //供货商成交额
        $array['supplier']['make_a_bargain'] = array_sum($price);
        //平台抽佣金
        $array['supplier']['commission'] = 0;
        //合作平台数量
        $array['platform']['platform_number'] = 1;
        //合作平台交子数量
        $array['platform']['jiaozi_number'] = 0;
        cache("statement",serialize($array),86400);
        }
        return json(['code'=>$this->t_code,'msg'=>"ok","data"=>$array]);
    }
}